//
//  HLHomeCellTopView.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/21.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLHomeCellTopView.h"
#import "UILabel+HLExtension.h"
#import <Masonry.h>
#import "HLStatuses.h"
#import <UIImageView+WebCache.h>
#import "HLHomeCellPicturesView.h"

@interface HLHomeCellTopView ()

//头像
@property (nonatomic,strong) UIImageView *iconView;
//认证头像
@property (nonatomic,strong) UIImageView *verified_Image;
//昵称
@property (nonatomic,strong) UILabel *nameLabel;
//会员图像
@property (nonatomic,strong) UIImageView *vipView;
//时间
@property (nonatomic,strong) UILabel *timeLabel;
//来源
@property (nonatomic,strong) UILabel *sourceLabel;
//文本内容
@property (nonatomic,strong) UILabel *contentLabel;
//图片视图
@property (nonatomic,strong) HLHomeCellPicturesView *topPicturesView;

//记录当前底部约束
@property (nonatomic,strong) MASConstraint *topViewBottomConstraint;

@end

@implementation HLHomeCellTopView

//重写构造方法
- (instancetype)init
{
    self = [super init];
    if (self) {
        //设置界面
        [self setupUI];
        
    }
    return self;
}

//设置界面
- (void)setupUI {
    //设置背景颜色
    self.backgroundColor = [UIColor whiteColor];
    //添加子控件
    [self addSubview:self.iconView];
    [self addSubview:self.verified_Image];
    [self addSubview:self.nameLabel];
    [self addSubview:self.vipView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.topPicturesView];
    
    //设置自动布局
    //头像
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).with.offset(HomeCellMargin);
        make.size.mas_equalTo(CGSizeMake(HomeCellIconWidth, HomeCellIconWidth));
    }];
    //认证图标
    [self.verified_Image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_right).with.offset(- HomeCellIconWidth * 0.1);
        make.centerY.equalTo(self.iconView.mas_bottom).with.offset(- HomeCellIconWidth * 0.1);
    }];
    //昵称
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(HomeCellMargin);
        make.top.equalTo(self.iconView);
    }];
    //vip图像
    [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView);
        make.left.equalTo(self.nameLabel.mas_right).offset(HomeCellMargin);
    }];
    //时间
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView);
        make.left.equalTo(self.iconView.mas_right).offset(HomeCellMargin);
    }];
    //来源
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView);
        make.left.equalTo(self.timeLabel.mas_right).offset(HomeCellMargin);
    }];
    //文本
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(HomeCellMargin);
        make.right.equalTo(self).offset(- HomeCellMargin);
        make.top.equalTo(self.iconView.mas_bottom).offset(HomeCellMargin);
    }];
    //图片
    [self.topPicturesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(HomeCellMargin);
        make.left.equalTo(self).offset(HomeCellMargin);
    }];
    
    //自身约束
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        self.topViewBottomConstraint = make.bottom.equalTo(self.topPicturesView).offset(HomeCellMargin);
    }];
}

//重写setter方法设置数据
- (void)setTopSatuses:(HLStatuses *)topSatuses {
    _topSatuses = topSatuses;
    //给子控件设置数据
    //头像
    [self.iconView sd_setImageWithURL:topSatuses.user.avatar_url];
    //认证
    self.verified_Image.image = topSatuses.user.verified_Image;
    //昵称
    self.nameLabel.text = topSatuses.user.name;
    //会员
    self.vipView.image = topSatuses.user.vipImage;
    //时间
//    self.timeLabel.text = topSatuses.created_at;
    //来源
//    self.sourceLabel.text = topSatuses.source;
    //文本
    self.contentLabel.text = topSatuses.text;
    //图片
    self.topPicturesView.imageUrlArr = topSatuses.imageUrlArr;
    //移除原有约束
    [self.topViewBottomConstraint uninstall];
    //根据有没有图片来更新底部约束
    if (topSatuses.imageUrlArr == nil || topSatuses.imageUrlArr.count == 0) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.topViewBottomConstraint = make.bottom.equalTo(self.contentLabel).offset(HomeCellMargin);
        }];
    }else {
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            self.topViewBottomConstraint = make.bottom.equalTo(self.topPicturesView).offset(HomeCellMargin);
        }];
    }
}

#pragma make - 懒加载
//头像
- (UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
        //设置圆角
        _iconView.layer.cornerRadius = HomeCellIconWidth * 0.5;
        _iconView.layer.masksToBounds = YES;
    }
    return _iconView;
}
//认证
- (UIImageView *)verified_Image {
    if (_verified_Image == nil) {
        _verified_Image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_enterprise_vip"]];
    }
    return _verified_Image;
}
//昵称
- (UILabel *)nameLabel {
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc] initWithTitle:@"吴彦祖" withTitleFont:14 withTitleColor:[UIColor orangeColor]];
    }
    return _nameLabel;
}
//vip
- (UIImageView *)vipView {
    if (_vipView == nil) {
        _vipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership_expired"]];
    }
    return _vipView;
}
//时间
- (UILabel *)timeLabel {
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc] initWithTitle:@"11:11" withTitleFont:10 withTitleColor:[UIColor orangeColor]];
    }
    return _timeLabel;
}
//来源
- (UILabel *)sourceLabel {
    if (_sourceLabel == nil) {
        _sourceLabel = [[UILabel alloc] initWithTitle:@"火星" withTitleFont:10 withTitleColor:[UIColor orangeColor]];
    }
    return _sourceLabel;
}
//文本内容
- (UILabel *)contentLabel {
    if (_contentLabel == nil) {
        _contentLabel = [[UILabel alloc] initWithTitle:nil withTitleFont:14 withTitleColor:[UIColor lightGrayColor]];
    }
    return _contentLabel;
}
//图片
- (HLHomeCellPicturesView *)topPicturesView {
    if (_topPicturesView == nil) {
        _topPicturesView = [[HLHomeCellPicturesView alloc] init];
    }
    return _topPicturesView;
}

@end
